package com.fk.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fk.dao.impl.BannerDaoImpl;
import com.fk.entity.Banner;
import com.fk.uitl.BaseServlet;
import com.fk.uitl.Result;
import com.google.gson.Gson;

/**
 * Servlet implementation class AdminBannerListServler
 */
public class AdminBannerListServler extends BaseServlet {
	
	/**
	 * 查询轮播图
	 * localhost:8080/wuyijiuchaoliufushiguan/adminBannerList?reqName=getBannerList
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getBannerList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		BannerDaoImpl bDao = new BannerDaoImpl();	// 创建实体类对象
		List<Banner> banner = bDao.getBannerList();	// 调用方法
		
		response.setCharacterEncoding("UTF-8");	// 设置编码格式
		PrintWriter out = response.getWriter(); // out 对象
		
		Gson gson = new Gson();
		String res = gson.toJson(banner);
		out.print(res);
		
		out.flush();
		out.close();
		
		
		
	}
	/**
	 * 删除轮播图
	 * localhost:8080/wuyijiuchaoliufushiguan/adminBannerList?reqName=delGetBanner
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delGetBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int bId = Integer.parseInt(request.getParameter("bId"));
		
		BannerDaoImpl bDao = new BannerDaoImpl(); 	// 创建实体类对象
		int b = bDao.delBanner(bId);	// 调用删除方法 
		
		response.setCharacterEncoding("UTF-8");	 // 设置编码格式
		PrintWriter out = response.getWriter();	 // 创建out对象
		
		if(b>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		}else {
			out.print(Result.toClient(false,"删除失败", ""));
		}
		
		
		out.flush();
		out.close();
		
	
	}
	/**
	 * ɾ���ֲ�ͼ
	 * localhost:8080/wuyijiuchaoliufushiguan/adminBannerList?reqName=addBanner
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		BannerDaoImpl baDao = new BannerDaoImpl();//产品分类的是先类对象
		
		String bannerImg = request.getParameter("bannerImg");
		if (bannerImg!=null && !bannerImg.equals("")) {
			List<Banner> baList = baDao.getBannerByImg(bannerImg);
			if (baList.size() == 0) {
				Banner ba = new Banner(0,bannerImg);
				int b = baDao.addBanner(ba);
				if (b > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "照片已存在", ""));
			}
		}
		
		
		
		out.flush();
		out.close();
	}
	
}
